package org.example.service;

import org.example.model.WordTemplateReplace;
import org.example.model.insertpicture;
import org.example.model.xmlinsertpic;
import org.example.model.xmlreplace;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;

public class FileMergerApp extends JFrame {
    private JButton btnUploadDocx;
    private JTextField txtDocxPath;
    private JButton btnUploadData;
    private JTextField txtDataPath;
    private JButton btnGenerate;
    private JLabel lblStatus;

    public FileMergerApp() {
        setTitle("文件合并应用");
        setSize(800, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());

        // 上传.docx文档按钮
        btnUploadDocx = new JButton("上传.docx文档");
        btnUploadDocx.addActionListener(e -> {
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setFileFilter(new javax.swing.filechooser.FileNameExtensionFilter("DOCX files", "docx"));
            int result = fileChooser.showOpenDialog(null);
            if (result == JFileChooser.APPROVE_OPTION) {
                File file = fileChooser.getSelectedFile();
                txtDocxPath.setText(file.getAbsolutePath());
            }
        });

        // .docx文件路径文本框
        txtDocxPath = new JTextField(60);
        txtDocxPath.setEditable(false);

        // 上传JSON/XML文件按钮
        btnUploadData = new JButton("上传JSON/XML文件");
        btnUploadData.addActionListener(e -> {
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setFileFilter(new javax.swing.filechooser.FileNameExtensionFilter("XML & JSON files", "xml", "json"));
            int result = fileChooser.showOpenDialog(null);
            if (result == JFileChooser.APPROVE_OPTION) {
                File file = fileChooser.getSelectedFile();
                txtDataPath.setText(file.getAbsolutePath());
            }
        });

        // JSON/XML文件路径文本框
        txtDataPath = new JTextField(60);
        txtDataPath.setEditable(false);


        // 生成按钮
        btnGenerate = new JButton("生成output.docx");
        btnGenerate.addActionListener(e -> {
            String docxPath = txtDocxPath.getText();
            String dataPath = txtDataPath.getText();
            if (docxPath.isEmpty() || dataPath.isEmpty()) {
                JOptionPane.showMessageDialog(this, "请上传所有文件。");
                return;
            }
            String fileType = getFileExtension(dataPath);
            if (!fileType.equals("xml") && !fileType.equals("json")) {
                JOptionPane.showMessageDialog(this, "上传的文件必须是XML或JSON格式。");
                return;
            }
            // 调用replace方法处理文件
            replace(docxPath, dataPath, fileType);
            lblStatus.setText("output.docx文件已生成。");
            openGeneratedDocument("output.docx");
        });

        // 状态标签
        lblStatus = new JLabel();

        // 添加组件到界面
        add(btnUploadDocx);
        add(txtDocxPath);
        add(btnUploadData);
        add(txtDataPath);
        add(btnGenerate);
        add(lblStatus);
    }

    // 获取文件扩展名
    private String getFileExtension(String filePath) {
        int i = filePath.lastIndexOf('.');
        if (i > 0) {
            return filePath.substring(i + 1).toLowerCase();
        }
        return "";
    }

    // 替换方法，需要根据实际情况实现
    private void replace(String docxFile, String dataFile, String fileType) {

        if ("xml".equals(fileType)) {
            xmlreplace.replace(docxFile,dataFile);
            xmlinsertpic.insert();

        } else if ("json".equals(fileType)) {
            WordTemplateReplace.replace(docxFile,dataFile);
            insertpicture.insert();
        }

    }
    private void openGeneratedDocument(String filePath) {
        try {
            if (Desktop.isDesktopSupported()) {
                Desktop desktop = Desktop.getDesktop();
                File file = new File(filePath);
                if (file.exists()) {
                    desktop.open(file);
                } else {
                    JOptionPane.showMessageDialog(this, "生成的文件不存在。", "错误", JOptionPane.ERROR_MESSAGE);
                }
            } else {
                JOptionPane.showMessageDialog(this, "桌面操作不支持。", "错误", JOptionPane.ERROR_MESSAGE);
            }
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(this, "无法打开文件。", "错误", JOptionPane.ERROR_MESSAGE);
        }
    }



}